package com.tavenli.quartz;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.tavenli.controller.IndexController;
import com.tavenli.repository.RedisDaoF18Device;
import com.tavenli.services.DeviceCmdService;
import com.tavenli.services.F18DeviceService;
import com.tavenli.utils.ConstantsUtil;
import com.tavenli.utils.JsonDateValueProcessor;
import com.tavenli.utils.SpringBeanFactoryUtils;
import com.tavenli.vo.F18DeviceVo;

public class DeviceCmdQuartz {
	private static Logger logger = LoggerFactory.getLogger(IndexController.class);
	
	private SpringBeanFactoryUtils springBeanFactoryUtils;
	public SpringBeanFactoryUtils getSpringBeanFactoryUtils() {
		return springBeanFactoryUtils;
	}
	public void setSpringBeanFactoryUtils(
			SpringBeanFactoryUtils springBeanFactoryUtils) {
		this.springBeanFactoryUtils = springBeanFactoryUtils;
	}
	private RedisDaoF18Device getRedisDaoF18Device() {
		return (RedisDaoF18Device) springBeanFactoryUtils.getBean("redisDaoF18Device");
	}
	private DeviceCmdService getDeviceCmdService(){
		return (DeviceCmdService) springBeanFactoryUtils.getBean("deviceCmdService");
	}
	private F18DeviceService getF18DeviceService(){
		return (F18DeviceService) springBeanFactoryUtils.getBean("f18DeviceService");
	}
	private void dealDeviceLog() {
		System.out.println("开始执行处理接收到的设备数据");
		Set<String> keysSet=getRedisDaoF18Device().getKeysPattern(ConstantsUtil.REDIS_DEVICE_LOG_LIST);
		for (String key : keysSet) {  
			String logString = getRedisDaoF18Device().listRightPop(key);
			if (null != logString && (!logString.equals(""))) {
				// 解析人员数据，解析指纹数据
				String[] logArray = logString.split("\r\n");
				for (int i = 0; i < logArray.length; i++) {
					if (!logArray[i].equals("")) {
						logger.info(logArray[i]);
						getDeviceCmdService().dealOneDeviceLog(logArray[i],key.replace(ConstantsUtil.REDIS_DEVICE_LOG_LIST, ""));
					}
				}
			}
		} 
		
	}
	private void dealDeviceRet() {
		 System.out.println("处理设备命令执行结果");
		 String logString=getRedisDaoF18Device().listRightPop(ConstantsUtil.REDIS_DEVICE_RET_LIST); 
		 if (null!=logString&&(!logString.equals("")))
		 { 
			 String []retArray=logString.split("\r\n");
			 for (int j = 0; j < retArray.length; j++) {
				 logger.info(retArray[j]);
				 getDeviceCmdService().dealOneDeviceRet(retArray[j]);
			}
			 
		 }
	}
	
	public void updateDevStatus(){
		Map<Object, Object> map=getRedisDaoF18Device().hashGetAll(ConstantsUtil.REDIS_DEVICE_STATUS_HASH);
		if (null!=map) {
			for (Object key : map.keySet()) {
			    String value = (String) map.get(key);
			    JsonConfig jsonConfig = new JsonConfig();
		        jsonConfig.setRootClass(F18DeviceVo.class);
		        Map<String, Class> classMap = new HashMap<String, Class>();  
		        jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
		        classMap.put("jsonObject", F18DeviceVo.class); // 指定JsonRpcRequest的request字段的内部类型  
		        jsonConfig.setClassMap(classMap);
		        JSONObject f18DeviceObj = JSONObject.fromObject(value,jsonConfig);
		        F18DeviceVo f18DeviceVo =(F18DeviceVo) JSONObject.toBean(f18DeviceObj,jsonConfig);
			    Date lastUpdateDate=f18DeviceVo.getLastUpdateDate();
			    int isOnline= f18DeviceVo.getIsOnline();
			    if (isOnline>0) {
			    	long diff = (new Date()).getTime()-lastUpdateDate.getTime();
			    	if(diff>1000*60){
			    		String sn=f18DeviceVo.getDeviceSn();
			    		getF18DeviceService().UpdateDeviceOffLine(sn);
			    	}
			    	
				}
			}
		}
		System.out.println("定时器启动执行");
	}
	
}
